package cn.boodqian.airdata;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.Inflater;

import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class DecodeServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.setCharacterEncoding("UTF-8");

		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();


		Entity put = new Entity("Employee", "DESKEY");

		String[] deskey = doDecode();
		put.setProperty("deskey", deskey[0]);
		put.setProperty("desiv", deskey[1]);

		Key key = datastore.put(put);
		
		Entity get = null;
		try {
			get = datastore.get(key);
		} catch (EntityNotFoundException e) {
			e.printStackTrace();
			resp.getWriter().println("Error");
			return;
		}

		String ret = get.getProperties().toString();
		resp.getWriter().println(ret);
	}
	private String[] doDecode() {
		String ret[] = null;
		try{
			URL url = new URL("http://202.136.217.188:9090/ssfb/ssfb/querysite.swf");
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setConnectTimeout(15000);
			InputStream reader = connection.getInputStream();
			int total = connection.getContentLength();
			int sofar = 0;
			int len = 0;
			byte[] buffer = new byte[total+4096];
			System.out.println("total=" + total+",sofar="+sofar);
			while((len=reader.read(buffer, sofar, 4096)) > 0) {
				sofar += len;
				//System.out.println("len="+len+",sofar="+sofar);
			}

			System.out.println("total=" + total+",sofar="+sofar);

			Inflater decompresser = new Inflater();
			decompresser.setInput(buffer, 8, total);
			byte[] result = new byte[total*4];
			int resultLength = decompresser.inflate(result);
			decompresser.end();

			System.out.println("result=" + resultLength);

			int i=0,j=0;
			while(i<resultLength) {
				if(result[i]==8 && result[i+9]==8) {
					boolean inrange = true;
					boolean allchardot = true;
					for(j=i+1;j<i+9;j++) {
						if(!isAscii(result[j])) inrange=false;
						if(!(result[j]>='a' && result[j]<='z') && !(result[j]>='A' && result[j]<='Z') && result[j]!='.') allchardot=false;
					}
					if(!inrange || allchardot) {i+=9;continue;}
					inrange=true;
					allchardot=true;
					for(j=i+10;j<i+18;j++) {
						if(!(result[j]>=0x20 && result[j]<=0x7e)) inrange=false;
						if(!(result[j]>='a' && result[j]<='z') && !(result[j]>='A' && result[j]<='Z') && result[j]!='.') allchardot=false;
					}
					if(!inrange || allchardot) {i+=9;continue;}
					ret = new String[]{new String(result, i+1, 8), new String(result, i+10, 8)};
					System.out.println(new String(result, i+1, 8));
					System.out.println(new String(result, i+10, 8));
					break;
				}
				i++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	public static boolean isAscii(byte c) {
		if(c>=0x20 && c<=0x7e) return true;
		return false;
	}
	public static boolean isAllAscii(byte[] array, int offset, int len) {
		int index = offset;
		while(index<offset+len)
			if(!isAscii(array[index])) return false;
		return true;
	}
}
